<?php

$mcrp = './../';
include_once('./../functions/session.php');
include_once('./../functions/categories.php');	

if($mcuser->isadmin == 0) 
{
	header('Location: ../');
	exit;
}

if(isset($_POST['newcategory']))
{
	$ci = new CategoryInfos;
	CreateCategory($ci);
	header('Location: ?id=' . $ci->id);
}
else if(isset($_POST['modify']))
{
	$ci = new CategoryInfos;
	$ci = GetCategoryById($_GET['id']);
	$ci->title = $_POST['title'];
	$ci->description = $_POST['descr'];
	UpdateCategory($ci);
}
else if(isset($_POST['delete']))
{
	DeleteCategoryById($_GET['id']);
	unset($_GET['id']);
}

if(isset($_GET['id']))
{
	$c = GetCategoryById($_GET['id']);
}
else
{
	$categories = GetAllCategories();
}

include('./../skin/default.php');
include('./../skin/header.php');

?>

<div class="mc_box">
	<h3>Administration des catégories</h3>	
	<div class="mc_contest_view_text">	
		<?php if(!isset($_GET['id']))
		{
		?>
			<form method="post">
				<input type="submit" name="newcategory" value="Nouvelle catégorie" />
			</form>
			<ul>
		<?php
			foreach($categories as $cat)
			{
				echo '<li><a href="?id=' . $cat->id . '">' . stripslashes($cat->title) . '</a></li>';
			}
			echo '</ul>';
		}
		else
		{
		?>
			<form class="mc_connection_form" method="post">
				<table>
					<tr>
						<td><label for="title">Titre :</label></td>
						<td><input id="mc_title" class="mc_input_box" type="text" tabindex="1" name="title" size="30" value="<?php echo stripslashes($c->title); ?>" /><br /></td>
					</tr>
					<tr>
						<td><label for="descr">Description :</label></td>
						<td><textarea name="descr" id="mc_descr" class="mc_input_box" rows="6" cols="80" tabindex="2"><?php echo stripslashes($c->description); ?></textarea><br /></td>
					</tr>
					<tr>
						<td><input type="submit" name="modify" tabindex="2" value="Modifier" /></td>
						<td><input type="submit" name="delete" tabindex="3" value="Supprimer (irréversible)" /></td>
					</tr>
				</table>
				<input type="hidden" name="id" value="<?php echo $c->id; ?>" />
			</form>
			<a href="categories.php">Retour</a>
		<?php
		}
		?>
	</div>
</div>

<?php

include('./../skin/footer.php');

?>